<?php

class HttpLoginConnect {

private $login;
private $user;
private $pass;
private $form_user;
private $form_pass;
private $parametro_opcional;
private $comprovar;
private $cookie_active;
private $cookie_value;

public function login($login)
{
    $this->login = $login;
}

public function user_pass($usuario, $password)
{
    $this->user = $usuario;
    $this->pass = $password;
}

public function field_user($field_user)
{
    $this->form_user = $field_user;
}

public function field_pass($field_pass)
{
    $this->form_pass = $field_pass;
}

public function additional_parameter($par){
$this->parametro_opcional = $par;
}

public function check(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4');
curl_setopt($ch,CURLOPT_REFERER, 'http://myavscan.net/login.php');
curl_setopt($ch, CURLOPT_URL, $this->login);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HEADER,true);//Salida de las cabeseras
curl_setopt ($ch, CURLOPT_POST, true);

if(empty($this->parametro_opcional)){
curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->form_user."=".$this->user."&".$this->form_pass."=".$this->pass);
}else{
curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->form_user."=".$this->user."&".$this->form_pass."=".$this->pass."&".$this->parametro_opcional);
}
$header = curl_exec($ch);
//echo $header;
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == 302){
preg_match_all("/Set-Cookie:\s*([\w\=]+)[\s\;\n]*/i", $header, $cookie_valor, PREG_SET_ORDER);


$this->cookie_value .= $cookie_valor[1][1].';';


if(!empty($this->cookie_value)){
$this->comprovar = true;
}

}
curl_close($ch);
return $this->comprovar;
}

public function cookie_active($value){
if($value){
$this->cookie_active = true;
}else{
$this->cookie_active = false;
}
}

public function view_cookie(){
if($this->comprovar){
return $this->cookie_value;
}else{
return false;
}
}


public function get($site){
if($this->comprovar && $this->cookie_active){
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4');
curl_setopt($ch,CURLOPT_REFERER, 'http://myavscan.net/');
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie_value);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4');
curl_setopt($ch,CURLOPT_REFERER, 'http://myavscan.net/');
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}

public function send_file($site, $file) {
$file = array('file' => '@'.$file);
if($this->comprovar && $this->cookie_active){
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4');
curl_setopt($ch,CURLOPT_REFERER, 'http://myavscan.net/');
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $this->cookie_value);
curl_setopt($ch, CURLOPT_POSTFIELDS, $file);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}else{
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4');
curl_setopt($ch,CURLOPT_REFERER, 'http://myavscan.net/');
curl_setopt($ch, CURLOPT_URL, $site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}


}

?>